package com.team.panicgame.view.gareve;

import java.util.Collection;
import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.utils.Array;
import com.team.panicgame.GameConstants;
import com.team.panicgame.models.gareve.GareveWorld;
import com.team.panicgame.models.gareve.Ship;
import com.team.panicgame.models.gareve.ShipParticle;
import com.team.panicgame.models.gareve.WallSegment;

public class GareveRenderer {
	private static final String TAG = GareveRenderer.class.getSimpleName();
	
	private GareveWorld gameWorld;
	private OrthographicCamera camera;
	private ShapeRenderer shapeRenderer = new ShapeRenderer();
	private SpriteBatch spriteBatch = new SpriteBatch();
	private FPSLogger fpsLogger = new FPSLogger();
	
	//Sounds
	private Sound engineSound;
	private Sound explosionSound;
	
	//Textures
	private Animation testAnimation;
	
	
	public GareveRenderer(){
		loadResources();
		
		camera = new OrthographicCamera(GameConstants.GAME_WIDTH, GameConstants.GAME_HEIGHT);
		camera.position.x = GameConstants.GAME_WIDTH / 2f;
		camera.position.y = GameConstants.GAME_HEIGHT / 2f;
		camera.update();
	}
	
	public void loadResources(){
		loadTextures();
		loadSounds();
	}
	
	private void loadTextures(){
		Texture testTexture = new Texture(Gdx.files.internal("testSprite3.png"));
		TextureRegion textures[][] = TextureRegion.split(testTexture, testTexture.getWidth()/8, 64);
		
		float duration = 1f / 4f;
		Array<TextureRegion> testTextures = new Array<TextureRegion>();
		for(int i=0;i<8;i++)
			testTextures.add(textures[0][i]);
		
		testAnimation = new Animation(duration,testTextures,Animation.LOOP_PINGPONG);
	}
	private void loadSounds(){
		engineSound = Gdx.audio.newSound(Gdx.files.internal("sounds/shipEngine.mp3"));
		explosionSound = Gdx.audio.newSound(Gdx.files.internal("sounds/explosion.wav"));
	}
	
	public void resize(float width,float height){
	}
	
	public void setFirstGameWorld(GareveWorld gareveWorld){
		this.gameWorld = gareveWorld;
	}
	
	
	public void render(){		
		initCamera();
		
		renderShip(gameWorld.ship);
		if(gameWorld.ship.isAlive)
			renderShipParticles(gameWorld.shipParticles);

		renderWallSegments(gameWorld.wallSegments);

		if(gameWorld.playEngineSound)
			engineSound.play(0.2f, 0.5f, 0f);
		if(gameWorld.playExplosionSound)
			explosionSound.play(0.2f, 0.5f, 0f);
		
		//Test Code
		spriteBatch.begin();
			spriteBatch.draw(testAnimation.getKeyFrame(gameWorld.animationTime), 20, 20, 20, 20);
		spriteBatch.end();
	}
	
	private void renderWallSegments(Collection<WallSegment> wallSegments){
		shapeRenderer.begin(ShapeType.Line);
			
			for (WallSegment wall : wallSegments){
				if(wall.hadCollision)
					shapeRenderer.setColor(1f, 0f, 0f, 1f);
				else
					shapeRenderer.setColor(1f, 1f, 1f, 1f);
				shapeRenderer.line(wall.p1.x,wall.p1.y,wall.p2.x,wall.p2.y);
			}
		shapeRenderer.end();
	}
	
	private void renderShipParticles(Collection<ShipParticle> shipParticles){
		Gdx.gl.glEnable(GL10.GL_BLEND);
		shapeRenderer.begin(ShapeType.FilledRectangle);
		for (Iterator it = gameWorld.shipParticles.iterator(); it.hasNext();) {
			ShipParticle shipParticle = (ShipParticle) it.next();
			if(shipParticle.isAlive()){
				shapeRenderer.setColor(0f, 1f, 0f, shipParticle.alpha);
				shapeRenderer.filledRect(shipParticle.shape.x, shipParticle.shape.y,shipParticle.shape.width, shipParticle.shape.height);
			}else{
				it.remove();
			}
		}
		shapeRenderer.end();
		Gdx.gl.glDisable(GL10.GL_BLEND);
	}

	private void renderShip(Ship ship){
		shapeRenderer.identity();
		shapeRenderer.begin(ShapeType.Triangle);
			if(ship.isAlive)
				shapeRenderer.setColor(0f, 1f, 0f, 1f);
			else
				shapeRenderer.setColor(1f, 0f, 0f, 1f);
			/*
			shapeRenderer.translate(ship.shape.x, ship.shape.y, 0);
			shapeRenderer.rotate(0, 0, 1f, ship.getAngle());
			//shapeRenderer.triangle(ship.shape.x,ship.shape.y,ship.shape.x+Ship.WIDTH,ship.shape.y,ship.shape.x + Ship.WIDTH/2f,ship.shape.y + 2f*Ship.HEIGHT);
			shapeRenderer.triangle(-Ship.WIDTH/2f,-Ship.HEIGHT,Ship.WIDTH/2f,-Ship.HEIGHT,0 ,Ship.HEIGHT);
			*/
			shapeRenderer.triangle(ship.p1.x,ship.p1.y,ship.p2.x,ship.p2.y,ship.p3.x,ship.p3.y);
		shapeRenderer.end();
		shapeRenderer.identity();
	}
	
	public void initCamera(){
		camera.position.x = gameWorld.ship.shape.x;
		camera.position.y = gameWorld.ship.shape.y;
		camera.update();		
		
		shapeRenderer.setProjectionMatrix(camera.combined);
		spriteBatch.getProjectionMatrix().setToOrtho2D(0f, 0, GameConstants.GAME_WIDTH, GameConstants.GAME_HEIGHT);
		
		
		Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		fpsLogger.log();
	}
}
